package cn.snmp.mutual.nms;

import java.io.*;
import java.math.*;

public abstract class SnmpVar implements Serializable
{
    byte Type;
    byte varbindErrorVal;
    
    SnmpVar() {
    }
    
    public static SnmpVar createVariable(final String value, final byte valueType) throws SnmpException {
        if (valueType == 4) {
            return new SnmpString(value);
        }
        if (valueType == 3) {
            return new SnmpBitstring(value);
        }
        if (valueType == 69) {
            return new SnmpNsap(value);
        }
        if (valueType == 64) {
            return new SnmpIpAddress(value);
        }
        if (valueType == 68) {
            return new SnmpOpaque(value);
        }
        if (valueType == 6) {
            final SnmpOID snmpoid = new SnmpOID(value);
            if (snmpoid.value == null) {
                throw new SnmpException(SnmpUtils.getString("Invalid OID format: ") + " " + value);
            }
            return snmpoid;
        }
        else {
            if (valueType == 2) {
                int i;
                try {
                    i = new Integer(value);
                }
                catch (NumberFormatException _ex) {
                    throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                }
                return new SnmpInt(i);
            }
            if (valueType == 67) {
                long l;
                try {
                    l = new Long(value);
                }
                catch (NumberFormatException _ex2) {
                    throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                }
                return new SnmpTimeticks(l);
            }
            if (valueType == 65) {
                long l2;
                try {
                    l2 = new Long(value);
                }
                catch (NumberFormatException _ex2) {
                    throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                }
                return new SnmpCounter(l2);
            }
            if (valueType == 70) {
                final long[] al = new long[2];
                try {
                    final BigInteger biginteger = new BigInteger(value);
                    final int j = biginteger.bitLength();
                    if (j > 64 || biginteger.compareTo(new BigInteger("0")) == -1) {
                        throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                    }
                    BigInteger biginteger2 = biginteger;
                    biginteger2 = biginteger2.shiftRight(32);
                    al[1] = biginteger2.longValue();
                    biginteger2 = biginteger.and(new BigInteger("FFFFFFFF", 16));
                    al[0] = biginteger2.longValue();
                }
                catch (NumberFormatException _ex) {
                    throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                }
                return new SnmpCounter64(al);
            }
            if (valueType == 66) {
                long l3;
                try {
                    l3 = new Long(value);
                }
                catch (NumberFormatException _ex2) {
                    throw new SnmpException(SnmpUtils.getString("Invalid format for : ") + " " + value);
                }
                return new SnmpGauge(l3);
            }
            if (valueType == 5) {
                return new SnmpNull();
            }
            return null;
        }
    }
    
    abstract int encode(final byte[] p0, final int p1) throws ArrayIndexOutOfBoundsException;
    
    public byte getError() {
        return this.varbindErrorVal;
    }
    
    public byte getType() {
        return this.Type;
    }
    
    public String getTypeString() {
        if (this.Type == 2) {
            return "INTEGER";
        }
        if (this.Type == 4) {
            return "STRING";
        }
        if (this.Type == 5) {
            return "NULLOBJ";
        }
        if (this.Type == 6) {
            return "OBJID";
        }
        if (this.Type == 64) {
            return "IPADDRESS";
        }
        if (this.Type == 3) {
            return "BITSTRING";
        }
        if (this.Type == 65) {
            return "COUNTER";
        }
        if (this.Type == 66) {
            return "GAUGE";
        }
        if (this.Type == 66) {
            return "UNSIGNED32";
        }
        if (this.Type == 67) {
            return "TIMETICKS";
        }
        if (this.Type == 68) {
            return "OPAQUE";
        }
        if (this.Type == 69) {
            return "NSAP";
        }
        if (this.Type == 70) {
            return "COUNTER64";
        }
        if (this.Type == 71) {
            return "UINTEGER32";
        }
        return "UNKNOWN";
    }
    
    public abstract Object getVarObject();
    
    public abstract byte[] toBytes();
    
    @Override
    public abstract String toString();
    
    public abstract String toTagString();
    
    public abstract Object toValue();
}
